<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 100px;
            height: 100px;
            border: 3px solid red;
        }

        .b1 {
            border-radius: 50%;
        }

        .b2 {
            width: 200px;
            border-radius: 50%;
            border-color: green;
        }

        .b3 {
            width: 200px;
            border-color: blue;
        }

        .b4 {
            border-color: yellow;
        }

        .box {
            margin-left: 100px;
            min-width: 400px;
            min-height: 400px;
            border: 3px solid pink;
            display: flex;
            flex-wrap: wrap;
        }
    </style>

</head>

<body>
    <div class="b1" draggable="true"></div>
    <div class="b2" draggable="true"></div>
    <div class="b3" draggable="true"></div>
    <div class="b4" draggable="true"></div>
    <p class="box"></p>
    <script>
        var aDiv = document.querySelectorAll('div');
        var oP = document.querySelector('p');

        //绑定事件
        for (let i = 0; i < aDiv.length; i++) {
            //开始拖拽的事件
            aDiv[i].ondragstart = function (ev) {
                console.log(this.className);
                //将这个div的class保存到 transfer
                ev.dataTransfer.setData('myclass', this.className);
            }
        }

        //给目的地绑定事件
        oP.ondragover = function (ev) {
            ev.preventDefault();
        }
        oP.ondrop = function (ev) {
            console.log(666);
            //在接收dataTransfer 值
            //创建div,给属性
            var newDiv = document.createElement('div');
            newDiv.className = ev.dataTransfer.getData('myclass');
            oP.appendChild(newDiv);
        }
    </script>
</body>

</html>